package com.dm.logistics.conservemanage.service.impl;

import com.dm.logistics.conservemanage.mapper.ConseMapper;
import com.dm.logistics.conservemanage.service.ConseService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class ConseServiceImpl implements ConseService {

    @Autowired
    private ConseMapper conseMapper;

    @Override
    public Map<String, Object> queryAllConseDoc(Map<String, Object> map) {
        String parentstore = map.get("parentstore")!=null?map.get("parentstore").toString():null;
        String store = map.get("store")!=null?map.get("store").toString():null;
        if(parentstore==null){
            return null;
        }
        if(parentstore.equals("数信网办公室")||parentstore.equals("集团办公室")){
            if("全部".equals(store)){
                map.put("store","");
            }
        }else{
            map.put("store",parentstore);
        }
        List<Map<String,Object>> resultList = conseMapper.queryAllConseDocMapper(map);
        Double CostTotal = 0.0;
        for(int i=0;i<resultList.size();i++) {
            String DocStatus = resultList.get(i).get("DocStatus") != null ? resultList.get(i).get("DocStatus").toString() : null;
            if("2".equals(DocStatus)){
                resultList.get(i).put("DocStatus", "执行中");
            }
            if("3".equals(DocStatus)){
                resultList.get(i).put("DocStatus", "执行完毕");
            }
            String ConseType = resultList.get(i).get("ConseType") != null ? resultList.get(i).get("ConseType").toString() : null;
            if("1".equals(ConseType)){
                resultList.get(i).put("ConseType", "定期维保");
            }
            if("2".equals(ConseType)){
                resultList.get(i).put("ConseType", "更换备件");
            }
            if("3".equals(ConseType)){
                resultList.get(i).put("ConseType", "报废更换");
            }
            String StartTimeStr = resultList.get(i).get("StartTime")!=null?resultList.get(i).get("StartTime").toString():null;
            if(StartTimeStr!=null){
                String[] StartTimeStrArr = StartTimeStr.split(" ");
                resultList.get(i).put("StartTime",StartTimeStrArr[0]);
            }
            String EndTimeStr = resultList.get(i).get("EndTime")!=null?resultList.get(i).get("EndTime").toString():null;
            if(EndTimeStr!=null){
                String[] EndTimeStrArr = EndTimeStr.split(" ");
                resultList.get(i).put("EndTime",EndTimeStrArr[0]);
            }
            String BuyInTimeStr = resultList.get(i).get("BuyInTime")!=null?resultList.get(i).get("BuyInTime").toString():null;
            if(BuyInTimeStr !=null){
                String[] BuyInTimeStrArr = BuyInTimeStr.split(" ");
                resultList.get(i).put("BuyInTime",BuyInTimeStrArr[0]);
            }
            String CreateTimeStr = resultList.get(i).get("CreateTime") != null ? resultList.get(i).get("CreateTime").toString() : null;
            if(CreateTimeStr!=null){
                String[] CreateTimeStrStrArr = CreateTimeStr.split(" ");
                resultList.get(i).put("CreateTime", CreateTimeStrStrArr[0]);
            }
            String ConseTimeStr = resultList.get(i).get("ConseTime") != null ? resultList.get(i).get("ConseTime").toString() : null;
            if(ConseTimeStr!=null){
                String[] ConseTimeStrArr = ConseTimeStr.split(" ");
                resultList.get(i).put("ConseTime", ConseTimeStrArr[0]);
            }


            Double CostTotalTemp = Double.valueOf(resultList.get(i).get("Cost")!=null?resultList.get(i).get("Cost").toString():"0");
            CostTotal += CostTotalTemp;
        }
        Map<String,String> count = conseMapper.queryListCount(map);
        JSONArray Js = JSONArray.fromObject(resultList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", count.get("total"));
        jsonObject.put("rows", Js.toString());


        //添加页脚总计合计行
        Map<String,Object> tempCostTotal = new HashMap<String, Object>();
        tempCostTotal.put("ConseType","当前页合计");
        tempCostTotal.put("Cost",CostTotal);
        Double CostTotal2 = 0.0;
        List<Map<String,Object>> resultList2 = conseMapper.queryAllConseDocMapper2(map);
        for(int i=0;i<resultList2.size();i++){
            Double CostTotalTemp = Double.valueOf( resultList2.get(i).get("Cost")!=null? resultList2.get(i).get("Cost").toString():"0");
            CostTotal2 += CostTotalTemp;
        }
        Map<String,Object> tempCostTotal2 = new HashMap<String, Object>();
        tempCostTotal2.put("ConseType","总计");
        tempCostTotal2.put("Cost",CostTotal2);
        List<Map<String,Object>> footList = new ArrayList<Map<String,Object>>();
        footList.add(tempCostTotal);
        footList.add(tempCostTotal2);
        JSONArray footJs = JSONArray.fromObject(footList);
        jsonObject.put("footer",footJs.toString());

        Map<String,Object> tempMap = new HashMap<String, Object>();
        tempMap.put("serviceList",jsonObject);
        return tempMap;
    }

    /**
     * @Description 提交维修单
     * @Author zhangxue
     * @Date 17:10 2018/5/7
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> saveConseDoc(Map<String, Object> map) {
        Date date = new Date();
        map.put("DocStatus","2");
        map.put("CreateTime",date);
        String EquNo = map.get("EquNo")!=null?map.get("EquNo").toString():null;
        Map<String,Object> equMap = conseMapper.queryEquInfoByEquNo(map);
        String EquId = equMap!=null?equMap.get("ID").toString():null;
        if(EquId==null|"".equals(EquId)){
            map.put("status","提交失败，没有编号为"+EquNo+"资产");
        }else{
            map.put("EquId",EquId);
            int i = conseMapper.saveConseDocMapper(map);
            if(i==1){
                map.put("status","提交成功");
            }
        }
        return map;
    }

    /**
         * @Description 执行待执行维修单
         * @Author zhangxue
         * @Date 15:26 2018/5/8
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveConseDocAndConseCourse(Map<String, Object> map) {
        String conseType = map.get("ConseType")!=null?map.get("ConseType").toString():"定期维保";
        if("更换备件".equals(conseType)){
            map.put("ConseType","2");
        }else if("报废更换".equals(conseType)){
            map.put("ConseType","3");
        }else{
            map.put("ConseType","1");
            int j = conseMapper.updateBasticConseTimeMapper(map);
        }
        int i = conseMapper.updateDocStatusByDocNoMapper(map);

//        int j = conseMapper.saveConseCourseMapper(map);
        if(i==1){
            map.put("status","执行成功");
        }else{
            map.put("status","执行失败");
        }
        return map;
    }

    /**
         * @Description 查询执行完成的维修单
         * @Author zhangxue
         * @Date 16:59 2018/5/8
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> queryBasicEquAndConseInfos(Map<String, Object> map) {
        Map<String,Object> resultMap = conseMapper.queryBasicEquAndConseInfosMapper(map);
        return resultMap;
    }

    /**
         * @Description 刪除維修單
         * @Author zhangxue
         * @Date 11:24 2018/5/14
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> deleteConseveById(Map<String, Object> map) {
        int i = conseMapper.deleteConseveById(map);
        Map<String,Object> statusMap = new HashMap<String,Object>();
        if(i==1){
            statusMap.put("status",1);
        }else{
            statusMap.put("status",2);
        }
        return statusMap;
    }


    /**
     * @Description 导出Excel
     * @Author zhangxue
     * @Date 15:01 2018/6/14
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> CreateAssessExcel2(Map<String, Object> param) {
        List<Map<String,Object>> resultList = conseMapper.QueryExcelByConditionMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("data",resultList);
        return resultMap;
    }
}
